package com.geek.novel.fragment

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.geek.novel.R
import com.geek.novel.activity.base.BaseActivity
import com.geek.novel.theme.*
import com.geek.novel.ui.ReadView
import com.geek.novel.utils.BrightUtil
import com.geek.novel.utils.SPUtil
import com.geek.novel.view.seekbar.BubbleSeekBar
import kotlinx.android.synthetic.main.layout_viewer_menu_light_setting.*

/**
 * 阅读页面菜单、亮度设置
 */
class FragmentLightSetting : BaseFragment() {
	
	override fun layoutId(): Int = R.layout.layout_viewer_menu_light_setting
	override fun pageName(): String = "亮度设置"
	override fun fragmentName(): String = FragmentName

	/**主题按钮*/
	private val themeList = ArrayList<BaseTheme>()
	
	override fun initV() {
		//初始化亮度调节设置
		this.initLightSetting()
		//初始化主题
		this.initTheme()
	}

	/**初始化亮度调节设置*/
	private fun initLightSetting(){
		val context = requireContext() as BaseActivity

		//初始化显示亮度百分比
		val systemBrightPercent = initCurrentSystemBright(context)
		//设置进度条
		sb_light_setting.progress = systemBrightPercent

		//是否跟随系统亮度设置
		val isSystemBright = getSystemBright()
		switch_light_auto.isChecked = isSystemBright
		switch_light_auto.setOnCheckedChangeListener { _, isChecked ->
			setSystemBright(isChecked)
			if(isChecked){//设置为系统亮度
				BrightUtil.resetSystemBrightnessPercent(context)
			}else{//设置为当前选择的亮度
				initCurrentSystemBright(context)
			}
		}

		//设置拖动改变亮度的监听
		sb_light_setting.onProgressChangedListener = object: BubbleSeekBar.OnProgressChangedListener {
			override fun onProgressChanged(bubbleSeekBar: BubbleSeekBar?, progress: Int, progressFloat: Float, fromUser: Boolean) {
				//设置亮度
				BrightUtil.setSystemBrightnessPercent(context, progress)
				//设置跟随系统亮度为false
				setSystemBright(false)
				switch_light_auto.isChecked = false
				//保存拖动的亮度
				setSystemBrightPercent(progress)
			}
			override fun getProgressOnActionUp(bubbleSeekBar: BubbleSeekBar?, progress: Int, progressFloat: Float) {
			}
			override fun getProgressOnFinally(bubbleSeekBar: BubbleSeekBar?, progress: Int, progressFloat: Float, fromUser: Boolean) {
			}
		}

	}


	/**初始化主题*/
	private fun initTheme(){
		val context = requireContext()
		themeList.add(ThemeDefault(context))
		themeList.add(ThemeYellow(context))
		themeList.add(ThemePink(context))
		themeList.add(ThemeGrey(context))
		themeList.add(ThemeNight(context))
		themeList.add(ThemeDark(context))

		//添加到布局中
		layout_grid_theme_container.adapter = ThemeGridViewAdapter(themeList)
		layout_grid_theme_container.setOnItemClickListener { _, _, position, _ ->
			themeList[position].toggleTheme()
		}
	}

	/**销毁时、移除控件的监听*/
	override fun onDestroy() {
		super.onDestroy()
		for (baseTheme in themeList) {
			baseTheme.onDestroy()
		}
	}


	private class ThemeGridViewAdapter(private val list: List<BaseTheme>): BaseAdapter(){
		override fun getCount(): Int {
			return list.size
		}
		override fun getItem(position: Int): Any {
			return position
		}
		override fun getItemId(position: Int): Long {
			return position.toLong()
		}
		override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
			val view = list[position]
			return view
		}

	}


	override fun myClick(v: View) {
	}
	
	companion object {
		const val FragmentName = "FragmentLightSetting"
		fun newInstance(): FragmentLightSetting{
			return FragmentLightSetting()
		}

		/**获取跟随系统亮度*/
		private fun getSystemBright(): Boolean{
			return SPUtil[SPUtil.book_system_bright, false, SPUtil.SP_SYSTEM_SETUP] as Boolean
		}
		/**设置跟随系统亮度*/
		private fun setSystemBright(bool: Boolean){
			SPUtil.put(SPUtil.book_system_bright, bool, SPUtil.SP_SYSTEM_SETUP)
		}

		/**获取跟随系统亮度百分比*/
		private fun getSystemBrightPercent(): Int{
			return SPUtil[SPUtil.book_system_bright_percent, -1, SPUtil.SP_SYSTEM_SETUP] as Int
		}
		/**设置跟随系统亮度百分比*/
		private fun setSystemBrightPercent(value: Int){
			SPUtil.put(SPUtil.book_system_bright_percent, value, SPUtil.SP_SYSTEM_SETUP)
		}

		/**初始化显示亮度*/
		fun initCurrentSystemBright(context: Activity): Int{
			var systemBrightPercent = this.getSystemBrightPercent()
			if(systemBrightPercent == -1){
				//获取系统亮度
				systemBrightPercent = BrightUtil.getSystemBrightnessPercent(context)
			}

			//是否跟随系统亮度设置
			val isSystemBright = getSystemBright()
			if(!isSystemBright){
				//设置亮度
				BrightUtil.setSystemBrightnessPercent(context, systemBrightPercent)
			}
			return systemBrightPercent
		}

	}
	
}
